﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class UserDao
    {


        public UserDao()
        {

            Mapper.CreateMap<Y_User, UserModel>();
        }
        public List<Y_User> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_User.Count();

                return edm.Y_User.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public UserModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_User.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_User, UserModel>(query);
            }

        }

        public UserModel UserLogin(string uName,string uPwd) {
            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_User.FirstOrDefault(s => s.UserName == uName&&s.Pwd==uPwd);
                return Mapper.Map<Y_User, UserModel>(query);
            }
        }


        public List<UserModel> GetList(string stwhr,string realname, int startIndex, int maxRow, out int recordcount)
        {
            using(YXQManageEntities edm=new YXQManageEntities()){
                var query = from s in edm.Y_User
                            select s;

                if (!string.IsNullOrEmpty(stwhr)) {
                    query = query.Where(s=>s.UserName.IndexOf(stwhr)>=0);
                }
                if (!string.IsNullOrEmpty(realname)) {
                    query = query.Where(s => s.RealName.IndexOf(stwhr) >= 0);
                }

                recordcount = query.Count();
                return Mapper.Map<List<Y_User>, List<UserModel>>(query.OrderByDescending(s=>s.Id).Skip(startIndex).Take(maxRow).ToList());


            }
        }


        public int Save(UserModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_User.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_User();
                    query.CreateTime = DateTime.Now;
                    edm.AddToY_User(query);

                }


                query.UserName = obj.UserName;

                query.Pwd = obj.Pwd;

                query.Tel = obj.Tel;

                query.Email = obj.Email;
                
                query.Status = obj.Status;

                query.LastLoginTime = obj.LastLoginTime;

                query.LoginCount = obj.LoginCount;

                query.RealName = obj.RealName;
                query.ThisLoginTime = obj.ThisLoginTime;
                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_User.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

